package Database

import (
	"SPC/Model"
	"fmt"
)

type PayrollDatabase struct {
	itsEmployees    map[int]Model.Employee //员工数据库
	itsUnionMembers map[int]int            //员工所属协会数据库
}

//用一个全局变量来模拟静态变量，所有p操作都是在操作它
var p *PayrollDatabase = &PayrollDatabase{
	itsEmployees: make(map[int]Model.Employee),
	itsUnionMembers: make(map[int]int),
}

//获取该全局对象
func GetPayrollDatabase() *PayrollDatabase {
	return p
}

//清除数据库中成员
func (p *PayrollDatabase) clear() {
	for k := range p.itsEmployees {
		delete(p.itsEmployees, k)
	}
	for k := range p.itsUnionMembers {
		delete(p.itsUnionMembers, k)
	}
}

//获取员工数据库中某一成员
func (p *PayrollDatabase) GetEmployee(empid int) Model.Employee {
	// val, ok := p.itsEmployees[empid]
	// if ok {
	// 	return val
	// } else {
	// 	return Model.Employee{} //没找到，返回空
	// }
	return p.itsEmployees[empid]
}

//添加员工
func (p *PayrollDatabase) AddEmployee(empid int, e Model.Employee) {
	p.itsEmployees[empid] = e
}

//删除员工
func (p *PayrollDatabase) DeleteEmployee(empid int) {
	delete(p.itsEmployees, empid)
}

//添加员工隶属协会
func (p *PayrollDatabase) AddUnionMember(memberid int, e Model.Employee) {
	var empid = e.GetEmpid()
	p.itsUnionMembers[memberid] = empid
}

//获取员工隶属协会(未判断空)
func (p *PayrollDatabase) GetUnionMember(memberid int) Model.Employee {
	empid := p.itsUnionMembers[memberid]
	return p.itsEmployees[empid]
}

//删除员工隶属协会
func (p *PayrollDatabase) RemoveUnionMember(memberid int) {
	delete(p.itsUnionMembers, memberid)
}

//获取所有的员工id
func (p *PayrollDatabase) GetAllEmployeeIds() []int {
	var empids = make([]int, 10) //创建一个切片
	for k, v := range p.itsEmployees {
		fmt.Println(k) //key
		fmt.Println(v) //value
		empids = append(empids, k)
	}
	return empids //返回所有员工的id
}
